package com.website.system.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.website.common.controller.BaseController;
import com.website.common.warpper.ResultMap;
import com.website.system.model.Area;
import com.website.system.model.AreaEasy;
import com.website.system.service.AreaEasyService;
import com.website.system.service.AreaService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 地区管理控制器
 *
 * @author 李星
 * @date 2020/10/19 9:31
 */
@Slf4j
@Controller
@RequestMapping("/manage/area")
public class AreaController extends BaseController {

    @Reference
    private AreaService areaService;
    @Reference
    private AreaEasyService areaEasyService;

    /**
     * 跳转到 后台-->地区信息管理
     */
    @RequestMapping("")
    public String areaList() {
        return "manage/area";
    }


    /**
     * 加载地区信息数据
     */
    @ResponseBody
    @RequestMapping("/areaTable")
    public ResultMap articleTable(@RequestParam(required = false) String content, @RequestParam Integer page,
                                  @RequestParam Integer limit) {
        log.info("/manage/area/areaTable input content:{}page:{},limit:{}",content,page,limit);
        ResultMap<List<Area>> resultMap = new ResultMap<>(SUCCESS_CODE, SUCCESS_MSG, null, null);
        Page<Area> pageInfo = new Page<>(page, limit);
        Wrapper<Area> wrapper = new EntityWrapper<>();
        if (null!=content &&  !"".equals(content)) {
            wrapper.like("title", content).or().like("content", content);
        }

        List<Area> areaList = areaService.selectPage(pageInfo, wrapper.orderBy("create_time", false)).getRecords();
        resultMap.setCount(areaService.selectCount(wrapper));

        for (Area area : areaList) {
            AreaEasy one = areaEasyService.selectById(area.getType());
            area.setAreaEasy(one);
        }
        resultMap.setCount(areaService.selectCount(wrapper));
        resultMap.setData(areaList);
        return resultMap;
    }

    /**
     * 获取所有区域类型
     * @return
     */
    @GetMapping("/allCity")
    @ResponseBody
    public List<AreaEasy> allCity(){
        return areaEasyService.selectList(null);
    }



    /**
     *获取区域类型
     */
    @RequestMapping("/toArea")
    @ResponseBody
    public ResultMap toArea(Model model) {
        log.info("/manage/toArea input model:{}",model.toString());
        ResultMap<List<AreaEasy>> resultMap = new ResultMap<>(SUCCESS_CODE, SUCCESS_MSG, null, null);
        Wrapper<Area> wrapper = new EntityWrapper<>();
        resultMap.setCount(areaService.selectCount(wrapper));
        resultMap.setData(areaEasyService.selectList(null));
        return resultMap;
    }

    /**
     * 跳转到 后台-->地区管理--> 添加
     */
    @RequestMapping("/toAreaAdd")
    public String toAreaAdd(Model model) {
        return "manage/areaAdd";
    }

    /**
     * 标签是否显示
     */
    @ResponseBody
    @RequestMapping("/updateAreaState")
    public String upAreaStarte(@RequestParam Integer state,@RequestParam Integer id){
        try {
            Area area = areaService.selectById(id);
            area.setState(state);
            areaService.updateById(area);
            return SUCCESS_MSG;
        }catch (Exception e){
            return ERROR_MSG;
        }
    }

    /**
     * 保存发布
     */
    @ResponseBody
    @RequestMapping(value = "/areaAdd")
    public String areaAdd(@RequestParam Integer type,@RequestParam String content,@RequestParam String title) {
        try {
            Area area = new Area();
            area.setState(SHOW_STATE);
            area.setTitle(title);
            area.setType(type);
            area.setContent(content);
            area.setCreateUser(getCurrentUser().getId());
            area.setCreateTime(new Date());
            area.setModifyTime(new Date());
            areaService.insert(area);
            return SUCCESS_MSG;
        }catch (Exception e){
            return ERROR_MSG;
        }
    }

    /**
     * 修改地区信息
     */
    @RequestMapping("/toAreaUpdate/{type}")
    public String getAssociation(Model model, @PathVariable Integer type) {
        Area area = areaService.selectById(type);
        AreaEasy areaEasy = areaEasyService.selectById(area.getType());
        area.setAreaEasy(areaEasy);
        model.addAttribute("area", area);
        return "manage/areaUpdate";
    }

    /**
     * 保存修改
     */
    @ResponseBody
    @RequestMapping("/areaUpdate")
    public String associationUpdate(@RequestParam Integer type,@RequestParam Integer id,
                                    @RequestParam String content,@RequestParam String title) {
        try{
            Area area = areaService.selectById(id);
            area.setType(type);
            area.setTitle(title);
            area.setContent(content);
            area.setModifyTime(new Date());
            areaService.updateById(area);
            return SUCCESS_MSG;
        }catch (Exception e){
            return ERROR_MSG;
        }
    }



    /**
     * 删除（后台-->地区管理-->删除操作）
     */
    @ResponseBody
    @RequestMapping(value = "/deleteArea",method = RequestMethod.POST)
    public String deleteArea(@Param("id") Integer id) {
        try{
            areaService.deleteById(id);
            return SUCCESS_MSG;
        }catch (Exception e){
            return ERROR_MSG;
        }
    }

}
